/*\
|*| 工具来源:https://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookie
|*|  :: cookies.js ::
|*|
|*|  A complete cookies reader/writer framework with full unicode support.
|*|
|*|  https://developer.mozilla.org/en-US/docs/DOM/document.cookie
|*|
|*|  This framework is released under the GNU Public License, version 3 or later.
|*|  http://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*|  Syntaxes:
|*|
|*|  * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|*|  * docCookies.getItem(name)
|*|  * docCookies.removeItem(name[, path], domain)
|*|  * docCookies.hasKey(name)
|*|  * docCookies.keys()
|*|  * docCookies.addTime({day:31})
|*| 
\*/
function docCookies() {}
docCookies.getItem = function (sKey) {
    return decodeURIComponent(
        document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")
    ) || null;
}
docCookies.setItem = function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
    var sExpires = "";
    if (vEnd) {
        switch (vEnd.constructor) {
            case Number:
                //如果是数字,单位默认是:秒
                sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
                break;
            case String:
                sExpires = "; expires=" + vEnd;
                break;
            case Date:
                sExpires = "; expires=" + vEnd.toUTCString();
                break;
        }
    }
    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "; path=/") + (bSecure ? "; secure" : "");
    return true;
}

docCookies.removeItem = function (sKey, sPath, sDomain) {
    if (!sKey || !docCookies.hasKey(sKey)) { return false; }
    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "; path=/");
    return true;
}

docCookies.hasKey = function (sKey) {
    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
}
docCookies.keys = function () {
    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
    return aKeys;
}
docCookies.addTime = function (option) {
    var __DEF = {year: 0,month: 0,day: 0,hour: 0,minute: 0,seconds: 0}
    var _option = Object.assign({}, __DEF, option)
    var dateObject = new Date()
    dateObject.setFullYear(dateObject.getFullYear() + _option.year)
    dateObject.setMonth(dateObject.getMonth() + _option.month)
    dateObject.setDate(dateObject.getDate() + _option.day)
    dateObject.setHours(dateObject.getHours() + _option.hour)
    dateObject.setMinutes(dateObject.getMinutes() + _option.minute)
    dateObject.setSeconds(dateObject.getSeconds() + _option.seconds)
    return dateObject
}
export default docCookies